package com.yanqu.road.server.timer.unionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.unionwar.UnionQunyingTimeInfo;
import com.yanqu.road.entity.activity.unionwar.enums.eTimeInfoPeriodType;
import com.yanqu.road.entity.activity.unionwar.result.CurrentTimeInfo;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.manager.unionwar.*;
import com.yanqu.road.utils.executor.GeneralTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 竞猜数据初始化定时器
 */
public class InitUnionWarGuessTask extends GeneralTask {
    private static final Logger logger = LogManager.getLogger(InitUnionWarGuessTask.class.getName());

    private boolean isRun = false;
    private Map<Integer, List<Integer>> isInitSecondStageOverMap = new HashMap<>();  //第二阶段竞猜数据是否初始化完成
    @Override
    public void doRun() {
        if (!isRun) {
            isRun = true;
            Map<Integer, ActivityInfo> activityInfoMap = CrossUnionWarActivityMgr.getOpenActivityMap();
            for (ActivityInfo activityInfo : activityInfoMap.values()) {
                if(!isInitSecondStageOverMap.containsKey(activityInfo.getActivityId())){
                    isInitSecondStageOverMap.put(activityInfo.getActivityId(), new ArrayList<>());
                }
                List<Integer> groupIdList = CrossUnionWarMgr.getGroupListByCrossServerId(activityInfo.getActivityId(), CrossBaseServer.getInstance().getServerId());
                for (int groupId : groupIdList) {
                    if (!isInitSecondStageOverMap.get(activityInfo.getActivityId()).contains(groupId)) {
                        CurrentTimeInfo currentTimeInfo = CrossUnionWarActivityMgr.getCurrentTimeInfo(activityInfo.getActivityId());
                        UnionQunyingTimeInfo timeInfo = currentTimeInfo.getTimeInfo();
                        if (timeInfo == null) {
                            if(currentTimeInfo.getPeriodType() == eTimeInfoPeriodType.ReceiveTime){
                                initFinalsGuessData(activityInfo, null, groupId);
                            }
                            continue;
                        }
                        int timeId = timeInfo.getTimeId();
                        //积分赛竞猜列表初始化 只要活动开始就可以进行
                        if (timeId > 0) {
                            CrossUnionWarGuessDataListModel guessDataListModel = CrossUnionWarGuessMgr.getCrossUnionWarGuessDataListModel(activityInfo.getActivityId(), 1, groupId);
                            if (guessDataListModel != null) {
                                //初始化竞猜数据
                                guessDataListModel.initGuessUnionInfoMap();
                            } else {
                                logger.error("unionWar first stage guess init error : model not init! activity {} groupId {}", activityInfo.getActivityId(), groupId);
                            }
                            //初始化排行榜数据
                            CrossUnionWarRankMgr.initUnionWarRankList(activityInfo.getActivityId(), 1, groupId);
                        }
                        //决赛竞猜列表初始化 决赛阶段 倒数第三轮次之后开始初始化
                        initFinalsGuessData(activityInfo, timeInfo, groupId);
                    }
                }
            }
            isRun = false;
        }
    }

    private void initFinalsGuessData(ActivityInfo activityInfo, UnionQunyingTimeInfo timeInfo, int groupId) {
        //决赛竞猜列表初始化 决赛阶段 倒数第三轮次之后开始初始化
        int type;
        if(timeInfo == null){
            type = 2;
            CrossUnionWarGuessDataListModel guessDataListModel = CrossUnionWarGuessMgr.getCrossUnionWarGuessDataListModel(activityInfo.getActivityId(), 1, groupId);
            if (guessDataListModel != null) {
                guessDataListModel.initGuessUnionInfoMap();
            }
        }else {
            type = timeInfo.getType();
        }
        if (type == 2) {
            CrossUnionWarGuessDataListModel guessDataListModel = CrossUnionWarGuessMgr.getCrossUnionWarGuessDataListModel(activityInfo.getActivityId(), 2, groupId);
            if (guessDataListModel != null) {
                boolean initSuccess = guessDataListModel.initGuessUnionInfoMap();
                if (initSuccess) {
                    isInitSecondStageOverMap.get(activityInfo.getActivityId()).add(groupId);
                }
            } else {
                logger.error("unionWar second stage guess init error : model not init!");
            }
        }
    }
}
